package gr.gsis.efmy.controller;

import gr.gsis.efmy.model.DataSerializer;
import gr.gsis.efmy.model.EarningsType;
import gr.gsis.efmy.model.PayrollTaxAggregate;
import gr.gsis.efmy.view.DetailsGroup;
import gr.gsis.efmy.view.MainView;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import org.eclipse.swt.widgets.TableItem;

public class MainController {
	
	private static Logger LOGGER = Logger.getLogger("MainController");
	
	private MainView mainView;
	
	private PayrollTaxAggregate model;
	
	public MainController(MainView view) {
		this.mainView = view;
	}
	
	public void save(String filePath) {
		LOGGER.info("Saving file...");
		
		if (null == model) {
			model = new PayrollTaxAggregate();
		}

		DetailsGroup dg = mainView.getDetailsGroup();
		
		//Create Header, Details, Totals
		model.header(
				"20100326", 
				Short.valueOf(dg.getTxtYear().getText())
			)
			.details(
				Short.valueOf(dg.getTxtYear().getText()),
				dg.getTxtLastName().getText(),
				dg.getTxtFirstName().getText(),
				"MiddleName",
				"0",
				dg.getTxtAfm().getText(),
				dg.getTxtCompanyName().getText(),
				dg.getTxtCity().getText(),
				dg.getTxtStreet().getText(),
				dg.getTxtStreet().getText(),
				Short.valueOf(dg.getTxtZipCode().getText())
			)
			.totals(
				1200.00,
				300.00,
				1000.00,
				100.00,
				1.18,
				0.00,
				98.00,
				0.00,
				0.00
			);
		
		TableItem[] items = mainView.getDataTable().getItems();
		int columns = mainView.getDataTable().getColumnCount();
		for (TableItem item : items) {
			StringBuffer sb = new StringBuffer();
			for (int i=0; i < columns; i++) {
				sb.append(item.getText(i) + ", ");
			}
			LOGGER.info("Item: " + sb.toString());
			
			//Create WageDetails
			model.addWageDetails(
				item.getText(0), //afm 
				item.getText(1), //lastname
				item.getText(2), //firstname
				item.getText(3), //middlename
				item.getText(4), //addrCity
				item.getText(5), //addrStreet
				item.getText(6), //addrNumber
				parseShort(item.getText(7)), //addrZipCode 
				EarningsType.ANADROMIKA_APODOXWN, //EarningsType.valueOf(item.getText(8)), //earningsType 
				parseDouble(item.getText(9)), //grossEarnings
				parseDouble(item.getText(10)), //stamp
				parseDouble(item.getText(11)), //ogaStamp
				parseDouble(item.getText(12)), //detentionsExcludingTax
				parseDouble(item.getText(13)), //netEarnings
				parseDouble(item.getText(14)), //withheldTax
				parseDouble(item.getText(15)), //tax
				item.getText(16), //branch 
				parseShort(item.getText(17)), //totalEmploymentDays
				parseDouble(item.getText(18)), //ogaWithheldTax
				parseDouble(item.getText(19)) //ogaTax
			);			
		}
		
		DataSerializer ds = DataSerializer.getInstance();
		
		try {
			ds.createFile(filePath, model);
		} catch (IOException e) {
			LOGGER.severe("Failed to create file. " + e.getMessage());
		}
	}
	
	private Double parseDouble(String doubleAsString) {
		return doubleAsString == null || doubleAsString.equals("") ? null : Double.valueOf(doubleAsString);
	}
	
	private Short parseShort(String shortAsString) {
		return shortAsString == null || shortAsString.equals("") ? null : Short.valueOf(shortAsString);
	}
	
	public void load() {
		
	}
	
	public void exit() {
		LOGGER.info("Exiting...");
		System.exit(0);
	}
}
